package edu.winona.csclub.testing.gui;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.lang.reflect.Method;

import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

import edu.winona.csclub.testing.core.ConfigFile;

public class TestCaseWindow extends JFrame implements ActionListener {
	JDesktopPane desktop;
	private static final String TESTPATH = "edu/winona/csclub/testing/testCode";
	public TestCaseWindow(){
		super("Test Harness");
				
		setLocation(25,35);
				
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()-50), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 70));

        desktop = new JDesktopPane(); 
        setContentPane(desktop);
        setJMenuBar(createMenuBar());

     //   desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		this.setVisible(true);
	}
	
    protected JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        //Set up the lone menu.
        JMenu menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_T);
        menuBar.add(menu);

        JMenuItem menuItem = new JMenuItem("New");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("news");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("Load");
        menuItem.setMnemonic(KeyEvent.VK_L);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_L, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("load");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Quit");
        menuItem.setMnemonic(KeyEvent.VK_Q);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("quit");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        return menuBar;
    }

	public void actionPerformed(ActionEvent e) {
		try{
		Method m = this.getClass().getMethod(e.getActionCommand(), new Class[]{});
		m.invoke(this,new Object[]{});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void quit(){
		System.exit(0);
	}
	
	public void load(){
		
	}
	
	public void news() throws Exception{
	    JFileChooser fc = new JFileChooser(new File(TESTPATH));
	    fc.setFileFilter(new ExtFileFilter("xml"));
	    fc.showOpenDialog(this);
	    File selFile = fc.getSelectedFile();

		TestInterfaceInternalFrame frame = new TestInterfaceInternalFrame(new ConfigFile(selFile));
		frame.setVisible(true);
        desktop.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
	}
	
	public static void main(String ... ar){
		new TestCaseWindow().setVisible(true);
	}
}
